package mn.idax.exchange.otc.interceptor;

import org.atmosphere.cpr.Action;
import org.atmosphere.cpr.AtmosphereInterceptorAdapter;
import org.atmosphere.cpr.AtmosphereResource;
import org.atmosphere.cpr.FrameworkConfig;
import org.atmosphere.util.Utils;
import org.springframework.web.util.WebUtils;

/**
 * @Auther: dison
 * @Date: 2018/8/30 14:51
 * @Description:
 */
public class AtmosphereInterceptor extends AtmosphereInterceptorAdapter {

    @Override
    public Action inspect(AtmosphereResource r) {

        if (Utils.webSocketMessage(r)) {
            return Action.CONTINUE;
        }

        if (!r.getRequest().localAttributes().containsKey(FrameworkConfig.SECURITY_SUBJECT)) {
//            try {
//                Subject currentUser = null;
//                if (r.transport().equals(TRANSPORT.WEBSOCKET)) {
//                    WebEnvironment env = WebUtils.getRequiredWebEnvironment(r.getAtmosphereConfig().getServletContext());
//                    currentUser = new WebSubject.Builder(env.getSecurityManager(), r.getRequest(), r.getResponse()).buildWebSubject();
//                } else {
//                    currentUser = SecurityUtils.getSubject();
//                }
//                if (currentUser != null) {
//                    r.getRequest().setAttribute(FrameworkConfig.SECURITY_SUBJECT, currentUser);
//                }
//            } catch (UnavailableSecurityManagerException ex) {
//                logger.info("Shiro Web Security : {}", ex.getMessage());
//            } catch (java.lang.IllegalStateException ex) {
//                logger.info("Shiro Web Environment : {}", ex.getMessage());
//            }
        }

        return Action.CONTINUE;
    }
}
